<template>
  <div
    v-ui-ref="vSetRef"
    class="relative flex flex-col w-full"
    :class="[
      'mt-' + (infoComponent.extension.topMargin && infoComponent.extension.topMargin.value),
      'mr-' + (infoComponent.extension.rightMargin && infoComponent.extension.rightMargin.value),
      'mb-' + (infoComponent.extension.bottomMargin && infoComponent.extension.bottomMargin.value),
      'ml-' + (infoComponent.extension.leftMargin && infoComponent.extension.leftMargin.value),
    ]"
  >
    <div
      class="boxSelect"
      :style="{
        '--color': infoComponent.extension.color?.value,
        '--width': `${infoComponent.extension.width?.value}px`,
        '--height': `${infoComponent.extension.height?.value}px`,
        '--font-size': `${infoComponent.extension.fontSize?.value}px`,
        '--voxcolor': `${infoComponent.extension.voxcolor?.value}`,
        display: 'flex',
        alignItems: infoComponent.extension.posi?.value,
      }"
    >
      <div class="input" @click="focusInput = !focusInput">
        <div id="input">{{ value }}</div>
        <div class="icon">
          <svg
            id="svg"
            class="svg"
            height="9"
            :style="{
              transform: focusInput ? 'rotateZ(45deg)' : 'rotateZ(225deg)',
            }"
            width="9"
          >
            <polygon points="9,0 9,9 0,9" style="fill: #fff" />
          </svg>
        </div>
      </div>
      <div
        class="bbbb"
        :style="{
          maxHeight: !focusInput ? '255px' : 0,
          overflow: 'hidden',
          position: 'absolute',
          top: Number(infoComponent.extension.height?.value) + 'px',
          transition: 'max-height 0.5s, border 1s',
        }"
      >
        <div
          class="option_box"
          :style="{
            maxHeight: infoComponent.extension.height?.value * 5 + 'px',
          }"
          @mouseleave="mouseLeave()"
        >
          <div id="optionBg" class="option_bg" :style="optionBgStyle"></div>
          <div id="dataBg" class="data_bg" :style="dataBgStyle">{{ text }}</div>
          <div v-for="(item, index) in list" :key="index" class="option" @click="choose(item)" @mouseover="mouseOver(index)">
            {{ item.label }}
            <svg v-if="selectValue == item.value" height="18" width="25">
              <polyline
                class="tick"
                fill="none"
                points="3,8 8,12 20,4"
                stroke="#22C5CD"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="4"
              />
            </svg>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ChartMixin } from '@ve/mixin'
  import cloneDeep from 'lodash/cloneDeep'
  export default defineComponent({
    name: 'UiAniSelect',
    mixins: [UiMixin, RefsInjectMixin, ChartMixin],
    inject: {
      hasPreview: { default: false },
    },
    props: {
      info: {
        required: true,
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        infoComponent: {},
        list: [],
        inter: null,
        focusInput: true,
        optionBgStyle: {
          top: 50 + 'px',
          opacity: 0,
        },
        dataBgStyle: {
          top: 50 + 'px',
          opacity: 0,
        },
        text: '',
        selectValue: '',
      }
    },
    watch: {
      info: {
        handler() {
          this.$nextTick(() => {
            this.infoComponent = cloneDeep(this.info)
            this.getRequestData()
            this.inter && (clearInterval(this.inter), (this.inter = null))
            if (this.info.extension.requestSet.value.loopRequest) {
              this.inter = setInterval(() => {
                this.getRequestData()
              }, this.info.extension.requestSet.value.loopTime)
            }
          })
        },
        deep: true,
      },
      set: {
        handler(val) {
          console.log(val, '-------')
          this.$forceUpdate()
        },
        deep: true,
      },
      // getSelectModule: {
      //   handler(val, oldVal) {
      //     if (!oldVal.extension) {
      //       if (pieOption[this.info.chartTypeValue]) {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = false)
      //       } else {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = true)
      //       }
      //     }
      //   },
      //   deep: true,
      // },
    },
    created() {
      this.infoComponent = cloneDeep(this.info)
    },
    mounted() {
      this.$nextTick(() => {
        this.getRequestData()
        if (this.info.extension.requestSet.value.loopRequest) {
          this.inter = setInterval(() => {
            this.getRequestData()
          }, this.info.extension.requestSet.value.loopTime)
        } else {
          this.inter && (clearInterval(this.inter), (this.inter = null))
        }
      })
    },
    deactivated() {
      console.warn('退出----------------------')
      this.inter && clearInterval(this.inter)
    },
    beforeUnmount() {
      this.inter && clearInterval(this.inter)
    },
    methods: {
      getRequestData() {
        this.$request(this.infoComponent.extension.requestSet.value, (res) => {
          this.list = res.map((item) => {
            return {
              label: item[this.infoComponent.extension.requestSet.value.fromTitle],
              value: item[this.infoComponent.extension.requestSet.value.fromValue],
            }
          })
        })
      },
      mouseOver(num) {
        console.log(num, Number(this.infoComponent.extension.height?.value))
        this.optionBgStyle = {
          top: num * (Number(this.infoComponent.extension.height?.value) + 10) + 5 + 'px',
          opacity: 1,
        }
        this.dataBgStyle = {
          opacity: 0,
          top: num * (Number(this.infoComponent.extension.height?.value) + 10) + 5 + 'px',
        }
      },
      choose(item) {
        this.text = item.label
        this.selectValue = item.value
        this.vuiComponents[this.info._id]?.self?.onChange()
        this.dataBgStyle = {
          opacity: 0.5,
          top: -(Number(this.infoComponent.extension.height?.value) + 10) + 'px',
        }
        setTimeout(() => {
          this.dataBgStyle.opacity = 0
          this.value = item.label
        }, 200)
      },
      mouseLeave() {
        this.optionBgStyle.opacity = 0
      },
    },
  })
</script>
<style scoped>
  .uiAniSelect {
    width: 100%;
    /* overflow: hidden; */
  }
  .boxSelect {
    display: flex;
    position: relative;
    flex-direction: column;
    height: auto;
  }
  .input {
    height: var(--height);
    width: var(--width);
    border-radius: 10px;
    border: 2px solid var(--color);
    cursor: text;
    color: #fff;
    font-weight: bold;
    font-size: var(--font-size);
    padding: 0 20px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    background: var(--voxcolor);
  }
  #input {
    flex: 1;
  }
  .option_box {
    max-height: 250px;
    width: var(--width);
    border: 1px solid var(--color);
    border-radius: 10px;
    margin: 5px 0 0;
    box-sizing: border-box;
    padding: 0 20px;
    position: relative;
    overflow: auto;
    background: var(--voxcolor);
    z-index: 999999;
  }
  .option_box::-webkit-scrollbar {
    display: none;
  }
  .option {
    height: calc(var(--height) + 10px);
    width: calc(var(--width) - 40px);
    box-sizing: border-box;
    font-weight: bold;
    font-size: var(--font-size);
    display: flex;
    align-items: center;
    color: #fff;
    cursor: pointer;
    z-index: 2;
    position: relative;
    display: flex;
    justify-content: space-between;
    user-select: none;
  }
  .option_bg {
    height: var(--height);
    width: calc(var(--width) - 20px);
    border-radius: 10px;
    background: rgba(156, 154, 154, 0.2);
    position: absolute;
    top: 50px;
    left: 10px;
    transition: top 0.5s, opacity 0.5s;
    opacity: 0;
    cursor: pointer;
    z-index: 1;
  }
  .data_bg {
    height: calc(var(--height) + 10px);
    width: calc(var(--width) - 20px);
    box-sizing: border-box;
    font-weight: bold;
    font-size: var(--font-size);
    display: flex;
    align-items: center;
    color: #fff;
    cursor: pointer;
    position: absolute;
    opacity: 0;
    transition: top 0.2s, opacity 0s;
  }
  .svg {
    transform: rotateZ(45deg);
    transform-origin: 50% 60%;
    transition: all 0.6s;
  }
  .tick {
    stroke-dasharray: 350;
    stroke-dashoffset: 350;
    animation: tick 8s ease-out;
    animation-fill-mode: forwards;
    animation-delay: 0.2s;
  }
  @keyframes tick {
    from {
      stroke-dashoffset: 350;
    }
    to {
      stroke-dashoffset: 0;
    }
  }
</style>
